package com.study.quiz.controller;

import com.study.quiz.dto.NotifictionDTO;
import com.study.quiz.dto.PageDTO;
import com.study.quiz.pojo.User;
import com.study.quiz.service.NotifictionService;
import com.study.quiz.service.QuestionService;
import com.study.quiz.utils.ErrorIndexUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author CJunGod
 * @create 2020-03-06 16:21
 **/
@Controller
public class ProFileController {

    @Resource
    private ErrorIndexUtil errorIndexUtil;

    @Resource
    private QuestionService questionService;

    @Resource
    private NotifictionService notifictionService;


    @GetMapping("/profile/{action}")
    public String goProFile(@PathVariable(name = "action") String action,
                            ModelMap modelMap, HttpServletRequest request,
                            @RequestParam(value = "page", defaultValue = "1") Integer page,
                            @RequestParam(value = "size", defaultValue = "6") Integer size) {
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            errorIndexUtil.errorIndex("用户未登录，请登录后重试");
            return "redirect:/";
        }

        if ("questions".equals(action)) {
            modelMap.put("section", "questions");
            modelMap.put("sectionName", "我发布的问题");
            PageDTO pageDto = questionService.findByUserId(user.getId(), page, size);
            modelMap.put("pageDto", pageDto);
            modelMap.put("page", pageDto.getPage());
        } else if ("replies".equals(action)) {
            PageDTO pageDTO = notifictionService.findByUserId(user.getId(), page, size);
            modelMap.put("pageDto", pageDTO);
            modelMap.put("page", pageDTO.getPage());
            modelMap.put("section", "replies");
            modelMap.put("sectionName", "最新回复");
        }

        return "profile";
    }
}
